<?php
/**
 * 菜系管理
 * @author shitou
 *
 */
class CuisineAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 添加菜系
     */
    public function add(){
    	$param = array(
    		"before" => "cuisine_add_before",
			"upload" => array("single"=>array('thumb')),
    		"return" => true
    	);
    	parent::do_add("Cuisine",$param);
    }
    
    /**
     * 修改菜系
     */
    public function edit(){
    	$param = array(
    		"before" => "cuisine_add_before",
			"upload" => array("single"=>array('thumb')),
    		"return" => true
    	);
    	parent::do_edit("Cuisine",$param);
    }
    /**
     * 菜系详情
     */
    public function detail(){
    	
    	$id = $this->_req("id");
    	
    	if($id){
    		$data = M("Cuisine")->find($id);
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 菜系列表
     */
    public function index(){
    	
    	$model = M("Cuisine");
    	
    	$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['role_view'] = $this->_req("role_view","intval",0);
    	$map = D('Outlet')->getRoleOutlet($cat);
    	
    	$desc      = $this->_get("orderDirection","trim","desc");;
    	$order     = $this->_get("orderName","trim","addtime");
    	$pageNum   = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage   = $this->_get("nowPage","intval",1);
    	$where     = $this->_req("where");
    	$package   = $this->_get("package","intval",0);
    	$map       = array();
    	 
    	if($where['keywords']){
    		$map['title|entitle'] = array("like","%".$where['keywords']."%");
    	}
    	if($where['city']){
    		$map['city'] = $where['city'];
    	}
    	if($cat['city_id']){
    		$map['city']  = $cat['city_id'];
    	}
    	
    	$map['isdel'] = 2;
    	
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    	->page($nowPage.','.$pageNum)->select();
    		
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    
    /**
     * 菜系列表无分页
     */
    public function view(){
    	
    	$city    = $this->_req("cityid",    "intval",0);
    	$city_id = $this->_req("city_id", "intval",0);
    	
    	if($city){
    		$con['city'] = $city;
    	}
    	
    	if($city_id){
    		$con['city'] = $city_id;
    	}
    	$con['isdel'] = 2;
    	
    	$data = M("Cuisine")->where($con)->order("sort desc")->select();
    	$this->success($data);
    	
    }
    /**
     * 删除餐厅
     */
    public function del(){
    	
    	$val   = $this->_req("val","trim","");
    	$model = M("Cuisine");
    	
    	if($val){
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = $model->save($map);
    		}
    	}
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的菜系");
    	}
    }
    
    /**
     * 所有菜系数据字典
     */
    public function dictCuisine(){
    	$lang    = getLang();
    	$cuisine = getDictByKey("Dict_Cuisine",$lang);
    	
    	$this->success($cuisine);
    }
    /**
     * 所有英文菜系数据字典
     */
    public function endictCuisine(){
    	//$lang    = getLang();
    	$cuisine = getDictByKey("Dict_Cuisine","en-us");

    	$this->success($cuisine);
    }

    
    /**
     * 所有地址数据字典
     */
    public function dictAddress(){
    	
    	$options['zh-cn'] = getDictByKey("Dict_address",'zh-cn');
    	$options['en-us'] = getDictByKey("Dict_address",'en-us');
    	
    	$this->success($options);
    }
    
    /**
     * 批量开启关闭免费配送
     */
    public function distribution(){
    	
    	$idStr = $this->_get("val");
    	$rel   = $this->_get("rel","trim","");
    	$msg   = "批量操作失败";
    	
    	if($rel=="mianfei"){
    		$map['is_exempt'] = 1;
    		$msg = "批量操作失败，当前没有餐厅需要批量开启免费配送";
    	}else{
    		$map['is_exempt'] = 2;
    		$msg = "批量操作失败，当前没有餐厅需要批量关闭免费配送";
    	}
    	
    	if($idStr){
    		$idArr = explode(',',$idStr);
    		foreach($idArr as $k=>$v){
    			$map['id']    = $v;
    			$data = M('Outlet')->save($map);
    		}
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($msg);
    	}
    }
   
}